package com.woniuxy.project115.kaer.user.consumer;

import com.woniuxy.project115.kaer.commons.util.SMSUtil;
import com.woniuxy.project115.kaer.user.util.RedisKey;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
public class SendCodeConsumer {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @RabbitListener(queues = "send_reg_code_queue")
    @RabbitHandler
    public void sendRegCode(String message)throws Exception {
        System.out.println("消费者1取出消息"+message);
        String code = SMSUtil.sendCode(message);
        //使用redis保存验证码
        stringRedisTemplate.opsForValue().set(RedisKey.REGISTER_CODE_KEY(message), code,3000, TimeUnit.SECONDS);
        //保存有效期key
        stringRedisTemplate.opsForValue().set(RedisKey.SEND_STATE(message),"",60,TimeUnit.SECONDS);
    }


    @RabbitListener(queues = "send_reg_code_queue")
    @RabbitHandler
    public void sendRegCode1(String message)throws Exception {
        System.out.println("消费者2取出消息"+message);
        String code = SMSUtil.sendCode(message);
        //使用redis保存验证码
        stringRedisTemplate.opsForValue().set(RedisKey.REGISTER_CODE_KEY(message), code,3000, TimeUnit.SECONDS);
        //保存有效期key
        stringRedisTemplate.opsForValue().set(RedisKey.SEND_STATE(message),"",60,TimeUnit.SECONDS);
    }

}
